package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.WmsTurnoverRule;
import com.jy.wms.service.WmsTurnoverRuleMiddleService;
import com.jy.wms.service.WmsTurnoverRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @ClassName: WmsTurnoverController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lh
 * @Date Mon Sep 04 11:31:18 GMT+08:00 2017
 */

@RestController
@RequestMapping("/wmsTurnOverRule")
@Api(value = "/wmsTurnOverRule", description = "关于周转规则操作")
public class WmsTurnoverController extends BaseController<WmsTurnoverRuleService, WmsTurnoverRule> {

    @Autowired
    private WmsTurnoverRuleMiddleService wmsTurnoverRuleMiddleService;

    @ApiOperation(value = "查询周转规则列表", response = JSONObject.class, notes = "查询周转规则列表")
    @PostMapping(value = "/list")
    public @ResponseBody
    JSONObject getWmsTurnoverRule(@RequestBody WmsTurnoverRule entity, HttpSession session) {
        JSONObject json = getJSONObject();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));
        try {
            Page<WmsTurnoverRule> list = baseService.baseSelectPage(entity,ckid);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "增加周转规则信息", response = JSONObject.class, notes = "增加周转规则信息")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject addWmsTurnoverRule(@RequestBody WmsTurnoverRule entity, HttpSession session) {
        JSONObject json = getJSONObject();
        entity.setId(UUIDUtils.uuid());
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));
        entity.setCkid(ckid);
        entity.setCreate_man(userinfo.getUser().getName());
        try {
            Integer count=baseService.getCountByMc(entity.getGzmc(),ckid);
            if(count==0){
                baseService.baseInsertSelective(entity);
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"增加周转规则成功","", JSONObject.fromObject(entity).toString());
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"增加周转规则失败","周转规则名称重复","");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"增加周转规则失败",e.getMessage(),"");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"增加周转规则成功",e.getMessage(), JSONObject.fromObject(entity).toString());

        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"增加周转规则失败",e.getMessage(),"");
        }
    }


    @ApiOperation(value = "周转规则管理查询所有", response = JSONObject.class, notes = "查询所有周转规则")
    @GetMapping(value = "/getDictionary")
    public @ResponseBody
    JSONArray getDictionary(HttpSession session) {
        JSONArray jsonArray = new JSONArray();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        try {
            jsonArray = baseService.baseSelectAll(String.valueOf(userinfo.getUser().getOrgId().get(0)));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }


    @ApiOperation(value = "周转规则管理查询所有及货主拥有的ids", response = JSONObject.class, notes = "查询所有周转规则及货主的周转规则，'sshzid'货主ID\"")
    @GetMapping(value = "/getDictionaryBySshzid/{sshzid}")
    public @ResponseBody
    JSONArray getDictionaryBySshzid(@PathVariable("sshzid") String sshzid, HttpSession session) throws Exception {
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = baseService.getDictionaryBySshzid(sshzid, String.valueOf(userinfo.getUser().getOrgId().get(0)));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }
}
